<?php

namespace App\Model;

use App\Traits\HasCompositePrimaryKey;
use Illuminate\Database\Eloquent\Model;

/**
 * @property string $url 网址
 * @property string $task_no 任务号
 * @property string $type 类型，每个任务号，可以解析的网址类型的标题
 * @property string $html 网址内容,ignore
 * @property integer $html_code 200
 * @property boolean $is_target 是否是目标网址
 * @property int $parse_status is_target=ture时的解析状态
 *
 * @property array $data 解析的结果
 */
class CommonSpiderItem extends Model
{
    //
    protected $table = "common_spider_items";
    const parse_status_init = 0;
    const parse_status_success = 1;
    const parse_status_ignore = 2;
    const parse_status_error = 3;
    //
    const html_code_init = 0;
    const html_code_success = 200;
    const html_code_404 = 404;
    const html_code_handle_error = -1;

    use HasCompositePrimaryKey;

    protected $primaryKey = ['url', 'task_no'];
    protected $casts = [
        'data' => 'array'
    ];

    public static function of(string $url, string $taskNo,bool $isTarget): CommonSpiderItem
    {
        $item = new CommonSpiderItem();
        $item->url = $url;
        $item->task_no = $taskNo;
        $item->is_target = $isTarget;
        $item->data = [];
        return $item;
    }
    public static function handledCode(): array
    {
        return [

            CommonSpiderItem::html_code_success,
            CommonSpiderItem::html_code_404,
        ];
    }

}
